const {debuger} = require("../common/debuger")
const { plugin } = require("../common/utils")
const { rockerCoordinate, findClosest, findFarthest, suitableTarget, distanceCalcul } = require("../common/compute")
const next = function(objects, index) {
    console.log("角色到门的距离：" + JSON.stringify(distanceCalcul(objects["coward"][0], objects["door"])))
    if (!objects || !objects["guidance"]) {
        console.info("未发现路标")
        return
    }
    let guidance = findFarthest(objects["coward"][0], objects["guidance"])
    console.info("找到最远距离箭头：" + JSON.stringify(guidance))
    if (objects["door"] && guidance) {
        let door = findClosest(guidance.target, objects["door"])
        console.info("箭头最近门:" + JSON.stringify(door))
        if (door.distance <= 300) {
            guidance = findClosest(objects["coward"][0], [door.target])
        } else {
            console.info("距离超过100")
        }
    }
    if (index == 0) {
        let o = rockerCoordinate({"h":133,"w":128,"x":266,"y":780}, guidance.angle)
        press(o.x, o.y, 100)
    }
    debuger.addLine({x1:guidance.p1.x,y1:guidance.p1.y,x2:guidance.p2.x,y2:guidance.p2.y})
}
exports.debug = function() {

    images.requestScreenCapture(true)
    for (let i = 0; i < 50; i++) {
        let capture = images.captureScreen()
        let bitmap = capture.getBitmap()
        let start = Date.now()
        console.log("截图宽:" + bitmap.getWidth() + ",高：" + bitmap.getHeight())
        //let result = plugin.blackScreenDetect(bitmap)//plugin.detect(bitmap, 0.8)
        let result = plugin.match(bitmap, ["dungeons/common/repair"], [0.3, 0.7, 0.8, 1])
        console.info("识别结果:" + JSON.stringify(result))
        console.info("识别耗时:" + (Date.now() - start))
        // debuger.refresh()
        // for (let obj of result) {
        //     debuger.add(obj)
        // }
        // let objects = {}
        // for (let obj of result) {
        //     if (objects[obj.label] == undefined) {
        //         objects[obj.label] = []
        //     }
        //     objects[obj.label].push(obj)
        // }
        // next(objects, i)
        // sleep(500)
    }
}

exports.match = function() {
    images.requestScreenCapture(true)
    let capture = captureScreen()
    let bitmap = capture.getBitmap()
    debuger.refresh()
    for (let index = 1; index <= 9; index++) {
        let target = plugin.match(bitmap, "dungeons/buwanjia/" + index, [0.5, 1, 0, 0.5])
        console.info("识别到确认按钮:"+ index + "" + JSON.stringify(target))
        if (target != null) {
            debuger.add(target)
            break
        }
    }
}